<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-05-06 00:05:39
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 工序产量
 */

namespace App\Models;

use App\Traits\CommonUserTrait;
use App\Traits\MaterialTrait;



class ProProcessRouteDetails extends BaseModel
    {
    use CommonUserTrait;
    use MaterialTrait;
    public $timestamps = true;
    public $table = "basedata_pro_process_route_details";
    protected $guarded = [];

    public function main()
        {
        return $this->belongsTo(ProProcessRoute::class);
        }
    public function process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function next_process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function front_process()
        {
        return $this->belongsTo(ProProcesses::class);
        }
    public function next_route()
        {
        return $this->belongsTo(get_class($this));
        }
    public function front_route()
        {
        return $this->belongsTo(get_class($this));
        }
    public function bom()
        {
        return $this->belongsTo(ProBom::class);
        }
    public function point_stick()
        {
        return $this->belongsTo(PointStick::class);
        }
    public function _deleted($model)
        {
        $gdata = OpenCardDetails::where('route_process_id', $model->id)->first();
        if ($gdata) {
            fail("{$model->rename}已被流转卡{$gdata->code}使用！无法修改！");
            }
        }
    public function _updated($model)
        {
        $changedata = $model->getDirty();

        $gdata = OpenCardDetails::where('route_process_id', $model->id)->first();
        if ($gdata) {
            foreach ($changedata as $key => $val) {
                if (in_array($key, ['next_process_id', 'process_id', 'front_process_id', 'sort', 'next_route_id', 'front_route_id']))
                    fail("{$model->rename}已被流转卡{$gdata->code}使用！无法修改！");
                }
            }
        }

    }
